//Author: Ferdinand Eisenkeil, Soeren Pirk
//Date  : 23.04.2014

#include "Scene.h"
#include "VertexBufferObjectAttribs.h"
#include "Shader.h"
#include "Box.h"
#include "Object.h"

Scene::Scene() 
{    
	culling = true;

    float s_terrain = 1.5;

    Box *ground = new Box(vec3(0, -0.01, 0), vec3(150, 0.01, 150), vec3(0.7, 0.7, 0.7));
        
    Object *terrain = new Object(vec3(0, 0, 0), vec3(s_terrain, s_terrain, s_terrain), "../Data/terrain.obj");

    m_objects.push_back(terrain);
	m_boxes.push_back(ground);
}

Scene::~Scene()
{
	for(int i=m_objects.size()-1; i>=0; --i)
    {
        Object *obj = m_objects[i];
        delete obj;
    }
    m_objects.clear();
	
	for(int i=m_boxes.size()-1; i>=0; --i)
    {
		Box *obj = m_boxes[i];
        delete obj;
    }
    m_boxes.clear();
}

void Scene::render(const Transform &trans)
{
    glPushAttrib(GL_ALL_ATTRIB_BITS);
    glPushClientAttrib(GL_CLIENT_ALL_ATTRIB_BITS);

    if(!m_showMesh)
    {
		glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
	}
    else
	{
        glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
	}

	glDisable(GL_CULL_FACE);

	for(int i=0; i<m_boxes.size(); ++i)
    {
        m_boxes[i]->render(trans);
    }
	for(int i=0; i<m_objects.size(); ++i)
    {
		m_objects[i]->culling = culling;
        m_objects[i]->render(trans);
    }

    glPopClientAttrib();
    glPopAttrib();
}

void Scene::reloadShader()
{
	for(int i=0; i<m_boxes.size(); ++i)
    {
        m_boxes[i]->reloadShader();
    }
	for(int i=0; i<m_objects.size(); ++i)
    {
		m_objects[i]->reloadShader();
	}
}

void Scene::update(double delta)
{    
}
